# -*- coding: utf-8 -*-
import json
import file_operate
import pathcfg
import time

field_template = "\n\t\t/// <summary>"
field_template += "\n\t\t/// {}"
field_template += "\n\t\t/// </summary>"
field_template += "\n\t\tpublic {} {};"

field_template2 = "\n\t\t{"
field_template2 += "\n\t\t\tget;"
field_template2 += "\n\t\t\tset;"
field_template2 += "\n\t\t}\n"

setfieldstr_template = "\n\t\t\t{} = ({})jsonData[\"{}\"];"
setfieldstr_template1 = "\n\t\t\t{} = GDUtil.toIntArray(jsonData[\"{}\"]);"
setfieldstr_template2 = "\n\t\t\t{} = GDUtil.toStringArray(jsonData[\"{}\"]);"
setfieldstr_template3 = "\n\t\t\t{} = GDUtil.toDynamicArray(jsonData[\"{}\"]);"
setfieldstr_template4 = "\n\t\t\t{} = GDUtil.toDynamicArray2(jsonData[\"{}\"]);"

ifield = "\n\t/** [注释] */"
ifield += "\n\tget [attr](): [type];"

field = "\n\tpublic get [attr](): [type] { return this.data.[attr]; }"

# 生成数据表ts文件模板
def create_table_class(jsondata):
    classname = jsondata["classname"]
    fieldnotes = jsondata["fieldnotes"]
    fieldnames = jsondata["fieldnames"]
    fieldtypes = jsondata["fieldtypests"]
    fieldstr = ""
    setfieldstr = ""
    for index in range(len(fieldtypes)):
        fieldnote = fieldnotes[index]
        fieldtype = fieldtypes[index]
        fieldname = fieldnames[index]
        i = ifield.replace("[注释]", fieldnote)
        i = i.replace("[attr]", fieldname)
        i = i.replace("[type]", fieldtype)
        fieldstr += i

        s = field.replace("[attr]", fieldname)
        s = s.replace("[type]", fieldtype)
        setfieldstr += s

    with open(pathcfg.data_table_class_path, 'r', encoding='utf-8') as file:
        file_content = file.read()

    tstr = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    content = file_content.replace('[CreateTime]', tstr)
    content = content.replace('[ClassName]', classname)
    content = content.replace('[Ifield]', fieldstr)
    content = content.replace('[field]', setfieldstr)
    # content = file_content % (tstr, classname, fieldstr, classname, setfieldstr)

    #输出cs文件模板
    file_operate.write_file(content, pathcfg.wcs_dir_path, classname, ".ts")
    print("Create" + classname + ".cs success")

# 生成数据表json文件
def create_table_json(jsondata):
    classname = jsondata["classname"]
    file_operate.write_file(json.dumps(jsondata["classdata"], ensure_ascii=False), pathcfg.wjson_dir_path, classname, ".json")
    print("Create " + classname + ".json success")

data_table_cfg_template = "\n\t\"%s\","

# 生成数据表名配置文件
def create_table_name_cfg(list):
    cfg_str = ""
    for index in range(len(list)):
        cfg_str += data_table_cfg_template % (list[index])

    with open(pathcfg.data_table_cfg_path, 'r', encoding='utf-8') as file:
        file_content = file.read()

    tstr = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    content = file_content % (tstr, cfg_str)

    file_operate.write_file(content, pathcfg.wcs_dir_path, "DataTableCfg", ".ts")
    # print("Create " + classname + ".json success")
